# coding=utf-8
from django.utils.translation import gettext_lazy as _


class APIException(Exception):
    default_detail = 'unknown'
    default_code = 0

    def __init__(self, status_code=0, detail=None):
        self.status_code = status_code or self.default_code
        self.detail = detail or self.default_detail


class LogoutException(APIException):
    def __init__(self, msg=None):
        self.status_code = 9999
        self.detail = msg


class CodeException(APIException):
    code_map = {
        -1: _("缺乏必要参数"),
        0: _("success"),
        9999: "{}",
    }

    def __init__(self, status_code=9999, msg=None):
        self.status_code = status_code
        self.detail = self.code_map.get(self.status_code, "unknown")
        if msg:
            self.detail = self.detail.format(msg)


class UserAvatarNotRealException(APIException):
    default_code = 200001
    default_detail = _("上传真实用户头像即可继续操作")


class UserIsBanException(APIException):
    default_code = 4000
    default_detail = _("该用户因发布广告或违规内容已被封禁")


class ModelUpdateForbiddenException(APIException):
    default_code = 1100
    default_detail = _("update is forbidden")


class ModelCreateForbiddenException(APIException):
    default_code = 1101
    default_detail = _("create is forbidden")


class PhoneCodeException(APIException):
    default_code = 2001
    default_detail = _("暂不支持该手机区号")


class PhoneRequestLimitedException(APIException):
    default_code = 2002
    default_detail = _("验证码已发送,请1分钟后再尝试重新发送")


class DeviceRequestLimitedException(APIException):
    default_code = 2003
    default_detail = _("请5分钟后再次尝试重新发送")


class PhoneCodeInvalidException(APIException):
    default_code = 2004
    default_detail = _("验证码不正确,请检查短信或重新发送")


class PhoneFormatInvalidException(APIException):
    default_code = 2005
    default_detail = _("手机号格式错误,请重新输入")


class NicknameExistsException(APIException):
    default_code = 2006
    default_detail = _("该昵称已存在，请更换后重试")


class AccountIsDisabled(APIException):
    default_code = 2007
    default_detail = _("该账号处于注销申请期，无法登录，最晚于申请提交后14个工作日处理完成")


class UserNotAuthException(APIException):
    default_code = 2008
    default_detail = _("")


class RegisterPhoneExistsException(APIException):
    default_code = 3001
    default_detail = ""

    def __init__(self, phone_number, phone_code, **kwargs):
        self.default_detail = _("手机号{0}已被注册,请直接登录或更改手机号后重新注册").format(phone_number)
        super(RegisterPhoneExistsException, self).__init__(**kwargs)


class LoginAuthException(APIException):
    default_code = 3002
    default_detail = _("手机号或密码错误, 请修改后重试")


class UserSearchNotFoundException(APIException):
    default_code = 3003
    default_detail = _("请检查手机号是否输入正确")


class OriginalPasswordInvalidException(APIException):
    default_code = 3004
    default_detail = _("原始密码错误,请重新输入")


class PasswordNotSetException(APIException):
    default_code = 3005
    default_detail = _("尚未设置密码，请使用验证码登录")


class TelecomUnsupportedException(APIException):
    default_code = 3006
    default_detail = _("暂不支持该运营商")


class FlashLoginException(APIException):
    default_code = 3007
    default_detail = _("一键登录失败，请使用验证码登录或稍后再试")


class PhoneIsBlackException(APIException):
    default_code = 3008
    default_detail = "手机号异常，如有问题请联系客服"


class InsufficientBalanceException(APIException):
    default_code = 6000
    default_detail = _("余额不足, 请充值后再试")


class WalletNotFoundException(APIException):
    default_code = 6001
    default_detail = _("钱包异常")


class OrderMyOwnServiceException(APIException):
    default_code = 6002
    default_detail = _("不能给自己下单")


class OrderAlreadyFinishedException(APIException):
    default_code = 6003
    default_detail = _("订单已完成，请勿重复操作")


class ServicePriceTooHighException(APIException):
    default_code = 6004
    default_detail = _("服务单价最高不能超过10000元")


class UnsupportedAlipayPlatformException(APIException):
    default_code = 6005
    default_detail = _("支付宝暂不支持该平台")


class UnsupportedPriceException(APIException):
    default_code = 6006
    default_detail = _("金额不正确，请重新输入")


class WechatPayAppOrderException(APIException):
    default_code = 6100
    default_detail = _("微信订单生成失败，请稍后再试")


class WechatPayH5OrderException(APIException):
    default_code = 6101
    default_detail = _("微信H5订单生成失败，请稍后再试")


class WechatPayJSOrderException(APIException):
    default_code = 6102
    default_detail = "微信JS订单生成失败，请稍后再试"


class WechatJSNeedsOpenIDException(APIException):
    default_code = 6104
    default_detail = "需要OpenID"


class AlipayCallbackSignException(APIException):
    default_code = 6201
    default_detail = _("支付宝回调验证失败")


class WechatCallbackSignException(APIException):
    default_code = 6202
    default_detail = _("微信回调验证失败")


class AppleSubscriptionUserException(APIException):
    default_code = 6203
    default_detail = ""

    def __init__(self, phone, **kwargs):
        self.default_detail = _('该VIP续订已绑定给用户{0}, 请切换账号后重试，或联系客服处理').format(phone)
        super(AppleSubscriptionUserException, self).__init__(**kwargs)


class InvalidNoneAppleProductException(APIException):
    default_code = 6204
    default_detail = _("只能续费非iap产品")


class ThirdPartOrderExpireException(APIException):
    default_code = 6205
    default_detail = _("该订单已超过验证时间")


class BalanceNotEnoughException(APIException):
    default_code = 6205
    default_detail = _("账户余额不足")


class SwipeCountInsufficientException(APIException):
    default_code = 7000
    default_detail = _("滑动次数不足")


class IDAuthAlreadyFinishedException(APIException):
    default_code = 7001
    default_detail = "已完成认证"


class IDAuthDeniedException(APIException):
    default_code = 7002
    default_detail = "当前身份信息异常，请联系客服"
